﻿Ext.define('page.Customer.Mod', {
    extend: 'Kwan.ex.ModuleInstance',
    metas: [
        { name: "NameCN", label: "姓名", sort: 1, ex_list: { width: 150 }, ex_edit: { allowBlank: false, width: 250} },
        { name: "Sex", label: "性别", sort: 2, ex_list: { width: 150, renderer: Kwan.colorSex }, ex_edit: { xtype: 'SimpleCombo', ex_combo: { ds: Kwan.tipSex, def: 0 }, cfg: { labelWidth: 70, width: 250}} },
        { name: "Phone", label: "电话号码", sort: 3, ex_edit: { width: 250} },
        { name: "IDType", label: "证件类型", sort: 4, ex_list: { width: 150, renderer: Kwan.colorIDType }, ex_edit: { xtype: 'SimpleCombo', ex_combo: { ds: Kwan.tipIDType, def: 0 }, cfg: { labelWidth: 70, width: 250}} },
        { name: "IDNO", label: "证件号码", sort: 5, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "Country", label: "国籍", sort: 6, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "Address", label: "住址", sort: 7, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "Memo", label: "备注", sort: 8, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "OrderID", label: "是否入住", sort: 9, ex_list: { width: 150, renderer: Kwan.isCheckIN} }
	    ],
    cfg: { start: 0, limit: 20, facade: 'facade.axd?s_action=Customer', def: 'Customer' },
    newData: [{ Address: '', Country: '中国', IDNO: '', IDType: 1, Level: 1, Memo: '', NameCN: '', Phone: '', Sex: 1}],
    getGrid: function () {
        var me = this,
            sorters = [{ property: 'OrderID', direction: 'DESC'}];
        var grid = Ext.create('Kwan.ex.Grid', {
            id: 'gpCustomer',
            region: 'center',
            store: Kwan.createStore(this.metas, this.cfg, sorters),
            columns: Kwan.createColumn(this.metas),
            title: '宾客列表',
            getQuery: function (clear) {
                var name = Ext.getCmp('qName').getValue(),
                    OrderID = Ext.getCmp('qOrderID').getValue();
                if (clear) {
                    OrderID = name = '';
                    Ext.getCmp('btnOrderID').toggle(false);
                }
                if (name.length > 0 || OrderID.length > 0) {
                    this.store.proxy.extraParams = { query1: 'NameCN=' + name + '&OrderID=' + OrderID };
                } else { this.store.proxy.extraParams = null; }
                this.store.load();
                this.filters.bindStore(this.store);
            },
            operBar: Ext.create('Kwan.ex.CrudBar', {
                pushItems: [
                    { xtype: 'hidden', id: 'qOrderID', name: 'qOrderID' },
                    { scale: 'medium', height: 25, width: 100, emptyText: " 搜索姓名..", xtype: 'textfield', id: 'qName', name: 'qName' },
                    { scale: 'medium', text: '搜索', icon: "/Admin/style/24Icons/jiancha.png", name: 'btnSearch',
                        handler: function () { Ext.getCmp('gpCustomer').getQuery(); }
                    },
                    { scale: 'medium', xtype: 'button', id: 'btnOrderID', icon: "/Admin/style/24Icons/application-exit.png", name: 'btnOrderID', iconCls: 'icon-delete', text: '在住宾客', enableToggle: true, handler: function () { Ext.getCmp('qOrderID').setValue(this.pressed ? '1' : ''); Ext.getCmp('gpCustomer').getQuery(); }
                    }
                ]
            })
        });
        grid.operBar.addListener('operAdd', function handler() {
            me.newData[0].CreatedDate = new Date();
            var cfg = { operType: 'add', saveUrl: me.cfg.facade, loadUrl: null, objId: null },
                win = me.getWin(cfg);
            win.show();
        });
        grid.operBar.addListener('operEdit', function handler() {
            var id = GetGridSelectedRowData(grid).EntityID;
            var cfg = { operType: 'edit', saveUrl: me.cfg.facade, loadUrl: me.cfg.facade, objId: id };
            var win = me.getWin(cfg);
            win.show();
        });
        grid.operBar.addListener('operDel', function handler() {
            var selects = grid.getSelectionModel().getSelection(), ids = [];
            if (selects.length > 0) {
                Ext.each(selects, function (item) {
                    ids.push(item.data.EntityID);
                });
                var cfg = { url: me.cfg.facade, id: ids.join(","), store: grid.getStore() };
                DoDelete(cfg);
            } else {
                ShowInfoMsg('系统提示', '请至少选择一项！');
            }
        });
        grid.operBar.addListener('operRefresh', function handler() {
            grid.getStore().load();
        });
        grid.addListener('itemdblclick', function handler(grid, rec, item, index, e) {
            var cfg = { operType: 'edit', saveUrl: me.cfg.facade, loadUrl: me.cfg.facade, objId: rec.data.EntityID };
            var win = me.getWin(cfg);
            win.show();
        });
        return grid;
    },
    getWin: function (cfg) {
        var win = Ext.create('Kwan.ex.FormWin', {
            title: '宾客管理',
            width: 350,
            height: 350,
            operType: cfg.operType, // 操作类型,
            fpID: this.cfg.def,
            formWinItems: Kwan.createFormItems(this.metas, this.cfg.def), // 表单包含的字段
            formColumns: 1, // 有几列
            formWinFieldWidth: 800, // 字段的总宽度
            saveUrl: cfg.saveUrl, // 提交的url地址
            loadUrl: cfg.loadUrl, // 读取数据的Url地址
            fPanel: 'gpCustomer', // 操作的父页面对象
            objId: cfg.objId, // 要修改的数据的id
            disableFields: [], // 编辑时要禁用的字段['Text', 'Code']
            newData: this.newData
        });
        return win;
    },
    run: function () {
        var me = this;
        me.center = me.getGrid();
        me.viewport.add([me.center]);
    }
});